package routes

import (
	"gin-epg/internal/app/common/middleware"
	"gin-epg/internal/app/controller"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
)

func SetRouter() *gin.Engine {
	r := gin.Default()

	// 配置 CORS 中间件
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"}, // 允许所有来源
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Length", "Content-Type", "Authorization", "token"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))

	/*
	 * epg_channel
	 */
	epgChannelGroup := r.Group("channel")
	{
		epgChannelGroup.POST("/channelPage", controller.GetEpgChannelPage)
		epgChannelGroup.POST("/channelList", controller.GetEpgChannelList)
		epgChannelGroup.GET("/channelDetail", controller.GetEpgChannelDetail)
		epgChannelGroup.POST("/channels", controller.CreateEpgChannel)
		epgChannelGroup.GET("/channelStd", controller.GetEpgChannelStd)
		epgChannelGroup.PUT("/channels/:id", controller.UpdateEpgChannel)
		epgChannelGroup.DELETE("/channels/:id", controller.DeleteEpgChannelById)
		epgChannelGroup.GET("/deleteChannelByName", middleware.TokenMiddleware(), controller.DeleteEpgChannelByName)
		epgChannelGroup.POST("/uploadLogo", middleware.TokenMiddleware(), controller.UploadEpgChannelLogoByName)
	}

	/*
	 * epg_channel_alias
	 */
	epgChannelAliasGroup := r.Group("channelAlias")
	{
		epgChannelAliasGroup.POST("/addAlias", middleware.TokenMiddleware(), controller.CreateEpgChannelAlias)
		epgChannelAliasGroup.GET("/channels", controller.GetEpgChannelAliasList)
		epgChannelAliasGroup.PUT("/channels/:id", controller.UpdateEpgChannelAlias)
		epgChannelAliasGroup.DELETE("/channels/:id", controller.DeleteEpgChannelAliasById)
	}

	epgGroup := r.Group("epg")
	{
		epgGroup.GET("/epginfo", controller.Epginfo)
		epgGroup.GET("/diyp", controller.Diyp)
		epgGroup.GET("/xmltv/:filename", controller.Xmltv)

		epgGroup.GET("/cron", controller.Cron)
	}

	toolGroup := r.Group("tool")
	{
		toolGroup.POST("/convPlaylist", controller.ToolConvPlaylist)
		toolGroup.GET("/convPlaylist", controller.ToolConvPlaylistGet)
		toolGroup.POST("/playlist2m3uMatch", controller.ToolPlaylist2m3uMatch)
		toolGroup.GET("/playlist2m3uMatch", controller.ToolPlaylist2m3uMatchGet)
		toolGroup.GET("/txt2tvbox", controller.ToolTxt2tvbox)
		toolGroup.POST("/mergetxt", controller.ToolMergeTxt)
		toolGroup.POST("/mcPtlist", controller.ToolMcPtList)
		toolGroup.GET("/mcchlistbypt", controller.ToolChListByPt)
		toolGroup.GET("/mcchlisttxtbypt", controller.ToolChTxtListByPt)
		toolGroup.GET("/batchDownloadMcchlist", controller.ToolBatchDownloadMcchlist)
		toolGroup.GET("/batchDownloadLogo", controller.ToolBatchDownloadLogo)
		toolGroup.GET("/batchDownloadLogoByProvince", controller.ToolBatchDownloadLogoByProvince)
		toolGroup.GET("/tvboxImgToStr", controller.TvboxImgToStr)
		toolGroup.GET("/tvboxUrlToStr", controller.TvboxUrlToStr)
		toolGroup.GET("/tvboxAes128CbcDecode", controller.TvboxAes128CbcDecode)
	}

	initGroup := r.Group("init")
	{
		initGroup.GET("/channel", controller.InitChannel)
		initGroup.GET("/logo", controller.InitLogo)
		initGroup.GET("/group", controller.InitGroup)
		initGroup.GET("/alias", controller.InitAlias)
	}

	// file 不分组
	r.GET("/file/*filename", controller.File)

	loginGroup := r.Group("login")
	{
		loginGroup.POST("/doLogin", controller.DoLogin)
		//logout
		loginGroup.GET("/logout", controller.Logout)
	}

	thirdPartGroup := r.Group("3part")
	{
		thirdPartGroup.GET("/getToken", controller.GetThirdPartToken)
		thirdPartGroup.GET("/saveToken", controller.SaveThirdPartToken)
		thirdPartGroup.GET("/deleteToken", controller.DeleteThirdPartToken)
	}
	return r
}
